<?php
namespace app\index\service;


use app\index\controller\Cert;
use app\index\controller\Flow;
use app\index\controller\Qiyuesuo;
use app\index\model\ArchiveModel;
use app\index\model\BidModel;
use app\index\model\CarModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractChuModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\DangJieListModel;
use app\index\model\DangModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginModel;
use app\index\model\EnginShuiModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowNodeModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowShenlogModel;
use app\index\model\FlowTalkModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\HouGonghuiListModel;
use app\index\model\HouJieModel;
use app\index\model\HouShouModel;
use app\index\model\NoticeModel;
use app\index\model\PaymentJieModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\QianfaModel;
use app\index\model\QianSuoModel;
use app\index\model\ReadsModel;
use app\index\model\SealGaiListModel;
use app\index\model\SuoYewuModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\relevan\CertRelevan;
use app\index\relevan\EnginRelevan;
use app\index\relevan\OtherRelevan;
use think\Db;
use think\Exception;

class FlowService extends CommonService
{

    public function get_flow_status(){
        return [
            0 => '进行中',
            1 => '审核完成（同意）',
            2 => '审核完成（不同意）',
            3 => '审核完成（超管结束）',
            5 => '审核完成（作废）',
        ];
    }

    public function get_flow_modename(){
        $list = [];

        $set = FlowSetModel::field('*,id as value')->select()->toArray();
        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($set as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children,'disabled'=>true];
            }
        }
        return $list;
    }

    public function save_bill($modenum, $mid){
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) throw new Exception("操作失败");

        $minfo = Db::name($set['modenum'])->where(['id'=>$mid])->find();
        if(!$minfo) throw new Exception("操作失败");
        $minfo['created_user_name'] = UserModel::get_realname_by_pk($minfo['created_user']);


        $bill = FlowBillModel::where(['modeid'=>$set['id'],'mid'=>$mid])->find();


        if(!$bill){
            $bill = $this->create_flow_bill($minfo, $set);
        }else{
            FlowBillModel::where(['id'=>$bill['id']])->update([
                'isturn' => $minfo['isturn'],
                'flowrelas' => $minfo['flowrelas'],
                'advaners' => $minfo['advaners'],
                'summary' => $this->create_flow_summary($set,$minfo),
                'bidid' => $this->create_flow_bidid($set['modenum'],$minfo),
                'enginid' => $this->create_flow_enginid($set['modenum'],$minfo),
                'enginame' => $this->create_flow_enginame($set['modenum'],$minfo),
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s"),
            ]);

            $bill = FlowBillModel::where(['id'=>$bill['id']])->find();
        }

        $bill['created_user_name'] = UserModel::get_realname_by_pk($bill['created_user']);

        Db::name($set['modenum'])->where(['id'=>$mid])->update(['billid'=>$bill['id']]);

        if($minfo['isturn'] == FlowbillModel::ISTURN_OFF){
            return true;
        }else{
            (new OptService())->add_flow_shenlog(['billid'=>$bill['id'],'name'=>'发起流程'], '提交申请',$this->userinfo['realname'], '', '','');
        }

        //判断流程模块是否开启
        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) throw new Exception("操作失败,流程未开启");

        //判断流程线是否有已发布的
        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) throw new Exception("操作失败,流程未设置");

        $step = json_decode($course['step'], true);
        if(!is_array($step)) throw new Exception("操作失败,流程未设置");
        FlowBillModel::where(['id'=>$bill['id']])->update(['step'=>$course['step']]);


        //删除 历史 审批节点
        FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->delete();
        //生成预审人 - 以审核节点 形式处理
        if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] > 0 && $bill['advaners']){
            $this->create_flow_advaners_node($bill);
        }


        if(isset($step[0]['data']['coursetype']) && $step[0]['data']['coursetype'] > 0){
            if(!$minfo['nexter']){
                throw new Exception("操作失败,请选择指定下一步审核人");
            }
            $nexterInfo = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'id'=>$minfo['nexter']])->find();
            if(!$nexterInfo){
                throw new Exception("操作失败,当前选择的下一步审核人不存在，请重新选择");
            }

            FlowBillModel::where(['id'=>$bill['id']])->update(['coursetype'=>1]);
        }

        //判断可建对象
        if(!$this->check_course_start_users($step)){
            throw new Exception("操作失败,您没有当前流程发起权限，请联系管理员");
        }


        //解析生成审批节点
        $this->create_flow_node($step, $minfo, $bill);
        $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] > 0 && $bill['advaners']){
            if(count($node) < 2){
                throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
            }
        }else{
            if(!$node){
                throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
            }
        }

        //生成审核任务
        $this->create_flow_node_task($node[0], $minfo, $bill);
        $daiqian_fyid = (new Qiyuesuo())->get_filetypeid($set['modenum'], 1);
        //判断是否需要契约锁
        if($set['modenum'] == 'qian_suo'){
            if($minfo['isturn'] > 0 && $minfo['state'] == 0){
                $filenum = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->count();
                if($filenum < 1){
                    throw new Exception('请上传待签署文件');
                }
                $yewuinfo = SuoYewuModel::where(['name'=>$minfo['qiantype']])->find();
                if(!$yewuinfo){
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
                $jsarr = [['username'=>$minfo['username'],'usertel'=>$minfo['usertel']]];
                $qysdraft = (new Qiyuesuo())->adddraft($yewuinfo['yewuid'], $minfo['name'], $yewuinfo['comname'], $jsarr);
                if($qysdraft && $qysdraft['code'] == 0){
                    $contractId = $qysdraft['result']['id'];
                    $fileinfo = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->select()->toArray();
                    $qysdicidArr = [];
                    foreach ($fileinfo as $ffvv){
                        $qysfile = (new Qiyuesuo())->addfile($contractId, $yewuinfo['yewuid'], $yewuinfo['comname'], $ffvv['oripath'], $ffvv['name'], $ffvv['ext']);
                        if($qysfile && $qysfile['code'] == 0){
                            $qysdicidArr[] = ['fileid'=>$ffvv['id'],'qysdicid'=>$qysfile['result']['documentId']];
                        }else{
                            throw new Exception("契约锁添加附件文档[".$ffvv['name']."]上传失败,请重新操作");
                        }
                    }

                    $qyscon = (new Qiyuesuo())->addcontract($contractId, $yewuinfo['yewuid'], $yewuinfo['comname']);
                    if($qyscon && $qyscon['code'] == 0){
                        FlowBillModel::where(['id'=>$bill['id']])->update(['qysconid'=>$contractId,'qysdicid'=>json_encode($qysdicidArr)]);
                        QianSuoModel::where(['id'=>$minfo['id']])->update([
                            'qysconid'=>$contractId,
                            'qysisyao'=>1,
                            'qysstate'=>0,
                        ]);
                    }else{
                        throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                    }
                }else{
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
            }
        }
        if($set['modenum'] == 'contract_nei'){
            if($minfo['isturn'] > 0 && $minfo['state'] == 0){
                $filenum = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->count();
                if($filenum < 1){
                    throw new Exception('请上传待签署文件');
                }
                $yewuinfo = SuoYewuModel::where(['name'=>$minfo['qiantype']])->find();
                if(!$yewuinfo){
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
                $jsarr = [['username'=>$minfo['gofuer'],'usertel'=>$minfo['gofutel']]];
                $qysdraft = (new Qiyuesuo())->adddraft($yewuinfo['yewuid'], $minfo['name'], $yewuinfo['comname'], $jsarr);
                if($qysdraft && $qysdraft['code'] == 0){
                    $contractId = $qysdraft['result']['id'];
                    $fileinfo = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->select()->toArray();
                    $qysdicidArr = [];
                    foreach ($fileinfo as $ffvv){
                        $qysfile = (new Qiyuesuo())->addfile($contractId, $yewuinfo['yewuid'], $yewuinfo['comname'], $ffvv['oripath'], $ffvv['name'], $ffvv['ext']);
                        if($qysfile && $qysfile['code'] == 0){
                            $qysdicidArr[] = ['fileid'=>$ffvv['id'],'qysdicid'=>$qysfile['result']['documentId']];
                        }else{
                            throw new Exception("契约锁添加附件文档[".$ffvv['name']."]上传失败,请重新操作");
                        }
                    }

                    $qyscon = (new Qiyuesuo())->addcontract($contractId, $yewuinfo['yewuid'], $yewuinfo['comname']);
                    if($qyscon && $qyscon['code'] == 0){
                        FlowBillModel::where(['id'=>$bill['id']])->update(['qysconid'=>$contractId,'qysdicid'=>json_encode($qysdicidArr)]);
                        ContractNeiModel::where(['id'=>$minfo['id']])->update([
                            'qysconid'=>$contractId,
                            'qysisyao'=>1,
                            'qysstate'=>0,
                        ]);
                    }else{
                        throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                    }
                }else{
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
            }
        }
        if($set['modenum'] == 'engin_shui'){
            if($minfo['isturn'] > 0 && $minfo['state'] == 0){
                $filenum = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->count();
                if($filenum < 1){
                    throw new Exception('请上传待签署文件');
                }
                $yewuinfo = SuoYewuModel::where(['name'=>$minfo['qiantype']])->find();
                if(!$yewuinfo){
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
                $jsarr = [['username'=>$minfo['chener'],'usertel'=>$minfo['chentel']]];
                $qysdraft = (new Qiyuesuo())->adddraft($yewuinfo['yewuid'], $minfo['name'], $yewuinfo['comname'], $jsarr);
                if($qysdraft && $qysdraft['code'] == 0){
                    $contractId = $qysdraft['result']['id'];
                    $fileinfo = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->select()->toArray();
                    $qysdicidArr = [];
                    foreach ($fileinfo as $ffvv){
                        $qysfile = (new Qiyuesuo())->addfile($contractId, $yewuinfo['yewuid'], $yewuinfo['comname'], $ffvv['oripath'], $ffvv['name'], $ffvv['ext']);
                        if($qysfile && $qysfile['code'] == 0){
                            $qysdicidArr[] = ['fileid'=>$ffvv['id'],'qysdicid'=>$qysfile['result']['documentId']];
                        }else{
                            throw new Exception("契约锁添加附件文档[".$ffvv['name']."]上传失败,请重新操作");
                        }
                    }

                    $qyscon = (new Qiyuesuo())->addcontract($contractId, $yewuinfo['yewuid'], $yewuinfo['comname']);
                    if($qyscon && $qyscon['code'] == 0){
                        FlowBillModel::where(['id'=>$bill['id']])->update(['qysconid'=>$contractId,'qysdicid'=>json_encode($qysdicidArr)]);
                        EnginShuiModel::where(['id'=>$minfo['id']])->update([
                            'qysconid'=>$contractId,
                            'qysisyao'=>1,
                            'qysstate'=>0,
                        ]);
                    }else{
                        throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                    }
                }else{
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
            }
        }
        if($set['modenum'] == 'hou_jie'){
            if($minfo['isturn'] > 0 && $minfo['state'] == 0){
                $filenum = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->count();
                if($filenum < 1){
                    throw new Exception('请上传待签署文件');
                }
                $yewuinfo = SuoYewuModel::where(['name'=>$minfo['qiantype']])->find();
                if(!$yewuinfo){
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
                $jsarr = [];
                $jsarr[] = ['username'=>$minfo['jieer'],'usertel'=>$minfo['jie_tel']];
                if($minfo['daner']){
                    $jsarr[] = ['username'=>$minfo['daner'],'usertel'=>$minfo['dan_tel']];
                }
                if($minfo['twoer']){
                    $jsarr[] = ['username'=>$minfo['twoer'],'usertel'=>$minfo['two_tel']];
                }
                $qysdraft = (new Qiyuesuo())->adddraft($yewuinfo['yewuid'], '借款登记-'.$minfo['jieer'], $yewuinfo['comname'], $jsarr);
                if($qysdraft && $qysdraft['code'] == 0){
                    $contractId = $qysdraft['result']['id'];
                    $fileinfo = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->select()->toArray();
                    $qysdicidArr = [];
                    foreach ($fileinfo as $ffvv){
                        $qysfile = (new Qiyuesuo())->addfile($contractId, $yewuinfo['yewuid'], $yewuinfo['comname'], $ffvv['oripath'], $ffvv['name'], $ffvv['ext']);
                        if($qysfile && $qysfile['code'] == 0){
                            $qysdicidArr[] = ['fileid'=>$ffvv['id'],'qysdicid'=>$qysfile['result']['documentId']];
                        }else{
                            throw new Exception("契约锁添加附件文档[".$ffvv['name']."]上传失败,请重新操作");
                        }
                    }

                    $qyscon = (new Qiyuesuo())->addcontract($contractId, $yewuinfo['yewuid'], $yewuinfo['comname']);
                    if($qyscon && $qyscon['code'] == 0){
                        FlowBillModel::where(['id'=>$bill['id']])->update(['qysconid'=>$contractId,'qysdicid'=>json_encode($qysdicidArr)]);
                        HouJieModel::where(['id'=>$minfo['id']])->update([
                            'qysconid'=>$contractId,
                            'qysisyao'=>1,
                            'qysstate'=>0,
                        ]);
                    }else{
                        throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                    }
                }else{
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
            }
        }
        if($set['modenum'] == 'hou_shou'){
            if($minfo['isturn'] > 0 && $minfo['state'] == 0){
                $filenum = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->count();
                if($filenum < 1){
                    throw new Exception('请上传待签署文件');
                }
                $yewuinfo = SuoYewuModel::where(['name'=>$minfo['qiantype']])->find();
                if(!$yewuinfo){
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }

                $jieinfo = HouJieModel::where(['id'=>$minfo['jieid']])->find();
                $jsarr = [['username'=>$jieinfo['jieer'],'usertel'=>$jieinfo['jie_tel']]];

                $qysdraft = (new Qiyuesuo())->adddraft($yewuinfo['yewuid'], '借款收条-'.$jieinfo['jieer'], $yewuinfo['comname'], $jsarr);
                if($qysdraft && $qysdraft['code'] == 0){
                    $contractId = $qysdraft['result']['id'];
                    $fileinfo = FileModel::where(['table'=>$set['modenum'],'mid'=>$minfo['id'],'typeid'=>$daiqian_fyid])->select()->toArray();
                    $qysdicidArr = [];
                    foreach ($fileinfo as $ffvv){
                        $qysfile = (new Qiyuesuo())->addfile($contractId, $yewuinfo['yewuid'], $yewuinfo['comname'], $ffvv['oripath'], $ffvv['name'], $ffvv['ext']);
                        if($qysfile && $qysfile['code'] == 0){
                            $qysdicidArr[] = ['fileid'=>$ffvv['id'],'qysdicid'=>$qysfile['result']['documentId']];
                        }else{
                            throw new Exception("契约锁添加附件文档[".$ffvv['name']."]上传失败,请重新操作");
                        }
                    }

                    $qyscon = (new Qiyuesuo())->addcontract($contractId, $yewuinfo['yewuid'], $yewuinfo['comname']);
                    if($qyscon && $qyscon['code'] == 0){
                        FlowBillModel::where(['id'=>$bill['id']])->update(['qysconid'=>$contractId,'qysdicid'=>json_encode($qysdicidArr)]);
                        HouShouModel::where(['id'=>$minfo['id']])->update([
                            'qysconid'=>$contractId,
                            'qysisyao'=>1,
                            'qysstate'=>0,
                        ]);
                    }else{
                        throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                    }
                }else{
                    throw new Exception("操作失败,契约锁操作错误，请联系管理员。");
                }
            }
        }

        return true;
    }

    public function save_bill_shen($modenum, $mid){
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) throw new Exception("操作失败");


        $minfo = Db::name($set['modenum'])->where(['id'=>$mid])->find();
        if(!$minfo) throw new Exception("操作失败");
        $minfo['created_user_name'] = UserModel::get_realname_by_pk($minfo['created_user']);

        $bill = FlowBillModel::where(['modeid'=>$set['id'],'mid'=>$mid])->find();
        if(!$bill){
            $bill = $this->create_flow_bill($minfo, $set);
        }else{
            FlowBillModel::where(['id'=>$bill['id']])->update([
                'flowrelas' => $minfo['flowrelas'],
                'advaners' => $minfo['advaners'],
                'summary' => $this->create_flow_summary($set, $minfo),
                'bidid' => $this->create_flow_bidid($set['modenum'], $minfo),
                'enginid' => $this->create_flow_enginid($set['modenum'], $minfo),
                'enginame' => $this->create_flow_enginame($set['modenum'], $minfo),
            ]);
            $bill = FlowBillModel::where(['id'=>$bill['id']])->find();
        }
        $bill['created_user_name'] = UserModel::get_realname_by_pk($bill['created_user']);

        Db::name($set['modenum'])->where(['id'=>$mid])->update(['billid'=>$bill['id']]);

        $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'opt'=>'tui','nodeid'=>0,'tuiid'=>0])
            ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();

        if(!$myTask) throw new Exception("操作失败");

        FlowTaskModel::where(['id'=>$myTask['id']])->update(['state'=>2, 'deleted_user'=>1]);

        if($myTask['tiaotype']  == 2){
            FlowTaskModel::where('id','in',$myTask['tingids'])->update(['state'=>1]);
        }else{
            //删除 历史 审批节点
            FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->update(['deleted_user'=>1]);
            FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->delete();

            //解析生成审批节点
            $step = json_decode($bill['step'], true);

            //生成预审人 - 以审核节点 形式处理
            if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] && $bill['advaners']){
                $this->create_flow_advaners_node($bill);
            }


            $this->create_flow_node($step, $minfo, $bill);
            $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
            if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] && $bill['advaners']){
                if(count($node) < 2){
                    throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
                }
            }else{
                if(!$node){
                    throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
                }
            }
            //生成审核任务
            $this->create_flow_node_task($node[0], $minfo, $bill);

        }
        FlowBillModel::where(['id'=>$bill['id']])->update(['recall'=>0]);
        FlowTaskShenModel::where(['billid'=>$bill['id']])->update(['recall'=>0]);
        (new OptService())->add_flow_shenlog($myTask, '申请人发起申请',$this->userinfo['realname'], '', '','');
        return true;
    }


    public function save_bill_summary($modenum, $mid){
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return true;

        $minfo = Db::name($set['modenum'])->where(['id'=>$mid])->find();
        if(!$minfo) return true;
        $minfo['created_user_name'] = UserModel::get_realname_by_pk($minfo['created_user']);

        $bill = FlowBillModel::where(['modeid'=>$set['id'],'mid'=>$mid])->find();
        if($bill){
            FlowBillModel::where(['id'=>$bill['id']])->update([
                'summary' => $this->create_flow_summary($set, $minfo),
            ]);
        }
        return true;
    }






    public function create_flow_node_task($data, $minfo, $bill){
        if($data['shentype'] != 'user'){
            $data['plain'] = 0;
        }
        $userArr = $this->get_flow_node_user($data, $minfo, $bill);

        FlowNodeModel::where(['id'=>$data['id']])->update(['final'=>implode(',', $userArr)]);


        $isadd = 1;
        if($data['signtype'] > 1){
            $isadd = 0;
        }

        if(!$userArr){
            $userArr = [0];
        }

        foreach ($userArr as $key=>$uid){
            $state = 1;
            if($data['plain'] > 0){
                if($key > 0){
                    $state = 0;
                }
            }
            if(!$uid){
                continue;
            }

            FlowTaskModel::insert([
                'comid' => $this->userinfo['comid'],
                'billid' => $bill['id'],
                'nodeid' => $data['id'],
                'name' => $data['name'],
                'signtype' => $data['signtype'],
                'plain' => $data['plain'],
                'uid' => $uid,
                'inituid' => $uid,
                'isadd' => $isadd,
                'rovedit' => isset($data['rovedit']) ? $data['rovedit'] : 0,
                'state' => $state,
                'created_at' => date("Y-m-d H:i:s"),
            ]);

            if($state == 1){
                (new WorkerService())->add_worker_bill($uid, 'shen', $bill);
            }
        }
        return true;
    }


    public function get_flow_node_user($data, $minfo, $bill){
        $userArr = [];
        if($data['shentype'] == 'user'){
            foreach (explode(',', $data['users']) as $_userid){
                $userArr[] = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
            }
        }elseif($data['shentype'] == 'dept'){
//            $deptArr = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
//                ->where('id','in', $data['users'])->column('id');
//            $userArr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
//                ->where('deptid','in', $deptArr)->group('id')->column('id');

            $fuerArr = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
                ->where('id','in', $data['users'])->column('fuer');
            $userArr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                ->where('id','in', $fuerArr)->group('id')->column('id');
        }elseif($data['shentype'] == 'role'){
            $roleArr = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where('id','in', $data['users'])->column('id');
            foreach ($roleArr as $_roleid){
                $arr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                    ->where(Db::raw("find_in_set(".$_roleid.",roleid)"))->column('id');
                $userArr = array_merge($userArr, $arr);
            }
        }elseif($data['shentype'] == 'gang'){
            $gangArr = UserGangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
                ->where('id','in', $data['users'])->column('id');
            foreach ($gangArr as $_gangid){
                $arr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                    ->where(Db::raw("find_in_set(".$_gangid.",gangid)"))->column('id');
                $userArr = array_merge($userArr, $arr);
            }
        }elseif($data['shentype'] == 'base'){
            $userArr = $this->get_flow_node_base_user($data, $minfo, $bill);
        }

        return array_unique($userArr);
    }


    public function get_flow_node_base_user($data, $minfo, $bill){
        $userArr = [];

        if($data['users'] == 'creater'){
            $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$bill['created_user']])->value('id');
        }

        if($data['users'] == 'credepter'){
            $credeptId = UserModel::where(['id'=>$bill['created_user']]) ->value('deptid');
            $fuer = DeptModel::where(['id'=>$credeptId])->value('fuer');
            $credepter = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuer])->value('id');
            if($credepter){
                $userArr[] = $credepter;
            }
        }

        if($data['users'] == 'fuzer'){
            if($bill['modenum'] == 'bid'){
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$minfo['fuzer']])->value('id');
            }
            if($bill['modenum'] == 'bid_book'){
//                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
//                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $bidid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('bidid');
                $fuzer = BidModel::where(['id'=>$bidid])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
        }

        if($data['users'] == 'booker'){
            if($bill['modenum'] == 'bid'){
                foreach (explode(',', $minfo['booker']) as $_userid){
                    $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
                }
            }
            if($bill['modenum'] == 'bid_book'){
//                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
//                foreach (explode(',', $booker) as $_userid){
//                    $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
//                }
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $bidid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('bidid');
                $booker = BidModel::where(['id'=>$bidid])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
        }


        if(in_array($data['users'], ['man_xiang','man_zhuan','man_zi','man_cheng','man_zhian'])){
            $users = '';
            if($bill['modenum'] == 'engin'){
                $users = $minfo[$data['users']];
            }
            if($bill['modenum'] == 'engin_junyan'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_junjie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_shi'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'kaoshi'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'kaobao'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_bu'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_cai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_cai_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_shi_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }

            if($bill['modenum'] == 'contract_bian'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_budget'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_budbian'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_cai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_ru'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_shen'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_ling'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_yong'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_jin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_lin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'subbao_shen'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'subbao_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_diary'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_speed'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_chan'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_shui'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_cha'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_stamp'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'visa'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_bao'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'income_huan'){
                $enginid = PaymentJieModel::where(['id'=>$minfo['jieid']])->value('enginid');
                $users = EnginModel::where(['id'=>$enginid])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_kai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_kua'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_shou'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'hou_gongshui'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'hou_gonghui'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'hou_gongzhi'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_put'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_kou'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_tax'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_neifu'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_yuzhi'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_neikou'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_engin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'income_engin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }

            if($bill['modenum'] == 'cert_use_bid'){
                $users = BidModel::where(['id'=>$minfo['bidid']])->value($data['users']);
            }
            if($bill['modenum'] == 'cert_use'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $enginid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('enginid');
                $users = EnginModel::where(['id'=>$enginid])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }

            $userArr = UserModel::where(['deleted_user'=>0,'status'=>1])->where('id','in',$users)->column('id');
        }
        return $userArr;
    }


    public function create_flow_node($data, $minfo, $bill){
        foreach ($data as $v){
            if($v['node'] == 'start'){
                if(isset($v['data']['coursetype']) && $v['data']['coursetype'] > 0){
                    $node[] = $this->get_peizhi_start_info($minfo, $bill);
                    break;
                }
            }

            if($v['node'] == 'rover'){
                $node[] = $this->get_peizhi_rover_info($v['data'], $bill);
            }

            if($v['node'] == 'term'){
                $termInfo = $this->get_peizhi_term_info($v['data'], $minfo, $bill);
                if(is_array($termInfo) && count($termInfo) > 0){
                    $this->create_flow_node($termInfo, $minfo, $bill);
                }
            }
        }
        return true;
    }

    public function get_peizhi_term_info($data, $minfo, $bill){
        $level = array_column($data,'level');
        array_multisort($level,SORT_ASC, $data);

        $minfo['created_user_old'] = $minfo['created_user'];
        $minfo['created_user'] = UserModel::get_realname_by_pk($minfo['created_user']);
        $minfo['created_dept'] = DeptModel::get_name_by_pk($minfo['deptid']);
        $factorObj = new FactorService();

        $res = false;
        $arrkey = 0;
        foreach ($data as $key => $item){
            if(!$item['list']){
                $res = true;
                $arrkey = $key;
                break;
            }else{
                $checkRes = call_user_func(array($factorObj, $bill['modenum']), $item['list'], $minfo);
                if($checkRes){
                    $res = true;
                    $arrkey = $key;
                    break;
                }
            }
        }

        if(!$res){
            throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
        }
        return $data[$key]['children'];
    }

    public function get_peizhi_start_info($minfo, $bill){
        $info = [];
        $info['name'] = '自由流程';
        $info['signtype'] = 1;
        $info['shentype'] = 'user';
        $info['users'] = $minfo['nexter'];
        $info['rovedit'] = 0;
        $info['plain'] = 0;
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 2;
        return FlowNodeModel::insert($info);
    }


    public function get_peizhi_rover_info($data, $bill){
        $info = [];
        if(!is_array($data)){
            throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
        }
        $info['name'] = $data['rovname'];
        $info['signtype'] = $data['signtype'];
        $info['shentype'] = $data['shentype'];
        $info['users'] = $data['users'];
        $info['rovedit'] = isset($data['rovedit']) ? $data['rovedit'] : 0;
        $info['plain'] = $data['plain'];
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 1;
        return FlowNodeModel::insert($info);
    }


    public function create_flow_advaners_node($bill){
        $info = [];
        $info['name'] = '预审';
        $info['signtype'] = 1;
        $info['shentype'] = 'user';
        $info['users'] = $bill['advaners'];
        $info['rovedit'] = 0;
        $info['plain'] = 0;
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 0;
        return FlowNodeModel::insert($info);
    }

    public function create_flow_bill($minfo, $set){
        $ins['comid'] = $minfo['comid'];
        $ins['chanid'] = $minfo['chanid'];
        $ins['deptid'] = $minfo['deptid'];
        $ins['flowuser'] = $minfo['flowuser'];
        $ins['flowdept'] = $minfo['flowdept'];
        $ins['flowrelas'] = $minfo['flowrelas'];
        $ins['advaners'] = $minfo['advaners'];
        $ins['sericnum'] = $this->create_flow_sericnum();
        $ins['bidid'] = $this->create_flow_bidid($set['modenum'], $minfo);
        $ins['enginid'] = $this->create_flow_enginid($set['modenum'], $minfo);
        $ins['enginame'] = $this->create_flow_enginame($set['modenum'], $minfo);
        $ins['summary'] = $this->create_flow_summary($set, $minfo);
        $ins['isturn'] = $minfo['isturn'];
        $ins['modeid'] = $set['id'];
        $ins['modenum'] = $set['modenum'];
        $ins['modename'] = $set['name'];
        $ins['mid'] = $minfo['id'];
        $ins['created_at'] = $minfo['created_at'];
        $ins['created_user'] = $this->userinfo['id'];
        $billid = FlowBillModel::insertGetId($ins);
        return FlowBillModel::where(['id'=>$billid])->find();
    }


    public function check_course_start_users($data){
        $shentype = isset($data[0]['data']['shentype']) ? $data[0]['data']['shentype'] : '';
        $users = isset($data[0]['data']['users']) ? $data[0]['data']['users'] : '';
        if(empty($shentype) || empty($users)){
            return true;
        }

        if($shentype == 'user'){
            if(in_array($this->userinfo['id'], explode(',', $users))){
                return true;
            }
        }
        if($shentype == 'dept'){
            if(in_array($this->userinfo['deptid'], explode(',', $users))){
                return true;
            }
        }
        if($shentype == 'role'){
            $userRole = explode(',', $this->userinfo['roleid']);
            $usersArr = explode(',', $users);
            foreach ($userRole as $v){
                if(in_array($v, $usersArr)){
                    return true;
                }
            }
        }
        if($shentype == 'gang'){
            $userGang = explode(',', $this->userinfo['gangid']);
            $usersArr = explode(',', $users);
            foreach ($userGang as $v){
                if(in_array($v, $usersArr)){
                    return true;
                }
            }
        }

        return false;
    }


    public function create_flow_sericnum(){
        $max = (new FlowBillModel())->get_max_sericnum($this->userinfo['comid']);
        if(!$max){
            return substr(date('Ymd'),2).'000001';
        }
        return $max+1;
    }
    public function create_flow_summary($set, $minfo){
        $str = '';

        //工程报备申请	流程名称 - 工程名称 - 业务归属
        if($set['modenum'] == 'bid'){
            $gui_unit_name = DeptModel::get_name_by_pk($minfo['gui_unit']);
            $str = $set['name'].' — '.$minfo['name'].' — '.$gui_unit_name;
        }
        //投标文件审察	流程名称 - 工程名称
        if($set['modenum'] == 'bid_book'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'];
        }
        //前期费收取	流程名称 - 工程名称 - 款项摘要 - 到账金额
        if($set['modenum'] == 'qian_shou'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //前期费收取-办事处	流程名称 - 工程负责人 - 款项摘要 - 到账金额
        if($set['modenum'] == 'qian_ban'){
            $str = $set['name'].' — '.$minfo['gofuer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //前期费支付	流程名称 - 工程名称 - 款项摘要 - 本次支付合计
        if($set['modenum'] == 'qian_zhi'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //前期费进项票	流程名称 - 工程名称 - 本次进项票金额
        if($set['modenum'] == 'qian_piao'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //投标保证金缴纳	流程名称 - 工程名称 - 本次支付合计
        if($set['modenum'] == 'tou_jiao'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //投标保证金返还	流程名称 - 工程名称 - 到账金额
        if($set['modenum'] == 'tou_fan'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //投标保函登记	流程名称 - 工程名称 - 保函金额
        if($set['modenum'] == 'tou_han'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }






        //借证申请 [申请人]的[流程名称]
        if($set['modenum'] == 'cert_borrow'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }
        //证件下载申请 [申请人]的[流程名称]
        if($set['modenum'] == 'cert_down'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }
        //投标用证申请	流程名称 - 用证项目
        if($set['modenum'] == 'cert_use_bid'){
            $engin = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' — '.$engin['name'];
        }
        //报建用证申请	流程名称 - 用证项目
        if($set['modenum'] == 'cert_use'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'];
        }







        //往来单位登记	流程名称 - 单位名称 - 所属分类
        if($set['modenum'] == 'dealcom'){
            $type_name = (new OptService())->get_base_showname('cai_dealcom_type', $minfo['type']);
            $str = $set['name'].' — '.$minfo['name'].' — '.$type_name;
        }
        //借款登记	流程名称 - 借款人 - 借款金额 - 用途
        if($set['modenum'] == 'hou_jie'){
            $str = $set['name'].' — '.$minfo['jieer'].' — '.$minfo['money'].'元'.' — '.$minfo['yong'];
        }
        //借款收条	流程名称 - 借款人 - 放款到账金额
        if($set['modenum'] == 'hou_shou'){
            $jieer = HouJieModel::where(['id'=>$minfo['jieid']])->value('jieer');
            $str = $set['name'].' — '.$jieer.' — '.$minfo['money'].'元';
        }
        //借款收条	流程名称 - 借款人 - 合同编号 - 本次放款金额
        if($set['modenum'] == 'hou_tiao'){
            $jieer = HouJieModel::where(['id'=>$minfo['jieid']])->value('jieer');
            $jienumber = HouJieModel::where(['id'=>$minfo['jieid']])->value('number');
            $str = $set['name'].' — '.$jieer.' — '.$jienumber.' — '.$minfo['money'].'元';
        }

        //费用报销	流程名称 - 付款对象 - 款项摘要 - 报销金额合计 - 工程名称
        if($set['modenum'] == 'hou_bao'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            if($engin){
                $str = $set['name'].' — '.$minfo['fuer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元'.' — '.$engin['name'];
            }else{
                $str = $set['name'].' — '.$minfo['fuer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
            }

        }







        //储备合同评审	流程名称 - 销售方联系人 - 合同名称 - 合同金额
        if($set['modenum'] == 'contract_chu'){
            $str = $set['name'].' — '.$minfo['lianer'].' — '.$minfo['name'].' — '.$minfo['money'].'元';
        }
        //储备税费支付	流程名称 - 销售方联系人 - 款项摘要 - 本次税款支付合计
        if($set['modenum'] == 'hou_chushui'){
            $str = $set['name'].' — '.$minfo['lianer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //储备合同结算	流程名称 - 合同名称 - 结算主题 - 结算金额
        if($set['modenum'] == 'contract_chu_jie'){
            $chuname = ContractChuModel::where(['id'=>$minfo['chuid']])->value('name');
            $str = $set['name'].' — '.$chuname.' — '.$minfo['title'].' — '.$minfo['money'].'元';
        }
        //储备进项支付	流程名称 - 销售方联系人 - 款项摘要 - 本次付款合计
        if($set['modenum'] == 'hou_chujin'){
            $str = $set['name'].' — '.$minfo['lianer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //储备进项票登记	流程名称 - 销售方联系人 - 本次进项票金额
        if($set['modenum'] == 'piao_chu'){
            $str = $set['name'].' — '.$minfo['lianer'].' — '.$minfo['money'].'元';
        }
        //资金调拨	流程名称 - 款项摘要 - 转账金额
        if($set['modenum'] == 'account_diao'){
            $str = $set['name'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //快递寄件	流程名称 - 收件人 - 快件内容 - 工程名称
        if($set['modenum'] == 'kuai_ji'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$minfo['shouer'].' — '.$minfo['nei'].' — '.$engin['name'];
        }
        //快递收件	流程名称 - 寄件人 - 快件内容 - 工程名称
        if($set['modenum'] == 'kuai_shou'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$minfo['shouer'].' — '.$minfo['nei'].' — '.$engin['name'];
        }
        //行政采购单	流程名称 - 采购主题 - 采购金额
        if($set['modenum'] == 'wu_cai'){
            $str = $set['name'].' — '.$minfo['title'].' — '.$minfo['money'].'元';
        }
        //物品领用申请	流程名称 - 领用主题
        if($set['modenum'] == 'wu_ling'){
            $str = $set['name'].' — '.$minfo['title'];
        }
        //图书借阅	流程名称 - 借阅人
        if($set['modenum'] == 'book_jie'){
            $jieer_name = UserModel::get_realname_by_pk($minfo['jieer']);
            $str = $set['name'].' — '.$jieer_name;
        }
        //物品入库申请	流程名称 - 入库主题 - 所属仓库
        if($set['modenum'] == 'wu_ru'){
            $cangku_name = (new OptService())->get_base_showname('wu_cangku', $minfo['cangku']);
            $str = $set['name'].' — '.$minfo['title'].' — '.$cangku_name;
        }
        //工作汇报	流程名称 - 申请部门 - 申请人 - 标题
        if($set['modenum'] == 'port'){
            $str = $set['name'].' — '.$minfo['flowdept'].' — '.$minfo['flowuser'].' — '.$minfo['title'];
        }
        //流程名称 - 姓名 - 沟通性质
        if($set['modenum'] == 'peng'){
            $gouxing_name = (new OptService())->get_base_showname('peng_gouxing', $minfo['gouxing']);
            $str = $set['name'].' — '.$minfo['name'].' — '.$gouxing_name;
        }
        //通知公告	流程名称 - 通知类型 - 标题 - 发布单位
        if($set['modenum'] == 'notice'){
            $tongtype_name = (new XingService())->get_notice_tongtype_show($minfo['tongtype']);
            $facom_name = DeptModel::get_name_by_pk($minfo['facom']);
            $str = $set['name'].' — '.$tongtype_name.' — '.$minfo['title'].' — '.$facom_name;
        }
        //文件签发 — 文件标题
        if($set['modenum'] == 'qianfa'){
            $str = $set['name'].' — '.$minfo['title'];
        }
        //员工工资表	流程名称 - 工资月份 - 工资标题
        if($set['modenum'] == 'wagyuan'){
            $str = $set['name'].' — '.$minfo['month'].' — '.$minfo['title'];
        }
        //建造师工资表	流程名称 - 工资月份 - 工资标题
        if($set['modenum'] == 'wagjian'){
            $str = $set['name'].' — '.$minfo['month'].' — '.$minfo['title'];
        }
        //通用审批流程
        if($set['modenum'] == 'tongyong'){
            $shilei = (new OptService())->get_base_showname('qi_tongyong_shilei', $minfo['shilei']);
            $str = $minfo['created_user_name'].'的'.$set['name'].' — '.$shilei;
        }
        //会议登记  会议登记 - 会议室 - 开始时间 - 发起人
        if($set['modenum'] == 'meet'){
            $room = (new OptService())->get_base_showname('qi_meet_room', $minfo['room']);
            $str = $set['name'].' — '.$room.' — '.$minfo['startdt'].' — '.$minfo['flowuser'];
        }
        //契约锁签章(其他) - 名称 - 工程名称
        if($set['modenum'] == 'qian_suo'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            if($engin){
                $str = $set['name'].' — '.$minfo['name'].' — '.$engin['name'];
            }else{
                $str = $set['name'].' — '.$minfo['name'];
            }
        }







        //工程承揽申请	流程名称 - 工程名称 - 承揽状态
        if($set['modenum'] == 'engin'){
            $lantake_name = (new EnginService())->get_engin_lantake_name($minfo['lantake']);
            $str = $set['name'].' — '.$minfo['name'].' — '.$lantake_name;
        }
        //工程管理文件汇编	流程名称 - 工程名称 - 工程负责人
        if($set['modenum'] == 'contract_nei'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$engin['gofuer'];
        }
        //区域合作协议	流程名称 - 协议名称 - 业务归属
        if($set['modenum'] == 'contract_qu'){
            $gui_unit_name = DeptModel::get_name_by_pk($minfo['gui_unit']);
            $str = $set['name'].' — '.$minfo['name'].' — '.$gui_unit_name;
        }
        //施工合同评审	流程名称 - 工程名称 - 合同金额
        if($set['modenum'] == 'contract_shi'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //工程纠纷 — 工程名称 — 纠纷名称
        if($set['modenum'] == 'kaoshi'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['name'];
        }
        //工程纠纷 — 工程名称 — 纠纷名称
        if($set['modenum'] == 'kaobao'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'];
        }

        //施工合同补充合同	流程名称 - 工程名称 - 合同金额
        if($set['modenum'] == 'contract_bu'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //施工合同结算	流程名称 - 工程名称 - 本次结算金额
        if($set['modenum'] == 'contract_shi_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //项目采购合同评审	流程名称 - 工程名称 - 采购类型 - 销售方 - 合同金额
        if($set['modenum'] == 'contract_cai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $caitype_name = (new OptService())->get_base_showname('engin_cai_caitype', $minfo['caitype']);
            $str = $set['name'].' — '.$engin['name'].' — '.$caitype_name.' — '.$minfo['gonger'].' — '.$minfo['money'].'元';
        }
        //采购合同结算	流程名称 - 工程名称 - 合同名称 - 销售方 - 本次结算金额
        if($set['modenum'] == 'contract_cai_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $caiinfo = ContractCaiModel::where(['id'=>$minfo['caiid']])->find();
            $str = $set['name'].' — '.$engin['name'].' — '.$caiinfo['name'].' — '.$caiinfo['gonger'].' — '.$minfo['money'].'元';
        }
        //工程回款	流程名称 - 工程名称 - 款项类型 - 到账金额
        if($set['modenum'] == 'hou_gonghui'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $shougonglei_name = (new OptService())->get_base_showname('cai_shougonglei', $minfo['shougonglei']);
            $str = $set['name'].' — '.$engin['name'].' — '.$shougonglei_name.' — '.$minfo['money'].'元';
        }
        //日常收款	流程名称 - 款项类型 - 收款对象 - 收款金额
        if($set['modenum'] == 'hou_dayshou'){
            $shouqilei_name = (new OptService())->get_base_showname('cai_shouqilei', $minfo['shouqilei']);
            $str = $set['name'].' — '.$shouqilei_name.' — '.$minfo['shouer'].' — '.$minfo['money'].'元';
        }
        //日常付款	流程名称 - 款项摘要 - 本次付款合计
        if($set['modenum'] == 'hou_dayzhi'){
            $str = $set['name'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //工程负责人预收款	流程名称 - 工程负责人- 款项摘要 - 到账金额
        if($set['modenum'] == 'hou_gongyu'){
            $str = $set['name'].' — '.$minfo['gofuer'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //工程付款	流程名称 - 工程名称 - 款项摘要 - 本次付款合计
        if($set['modenum'] == 'hou_gongzhi'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['zhai'].' — '.$minfo['money'].'元';
        }
        //进项税额抵扣退税申请	流程名称 - 工程名称 - 本次进项抵扣退税
        if($set['modenum'] == 'hou_gongshui'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //税款保证金承诺书	流程名称 - 工程名称 - 承诺人 - 税款保证金金额
        if($set['modenum'] == 'engin_shui'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['chener'].' — '.$minfo['baofee'].'元';
        }
        //销项票开具	流程名称 - 工程名称 - 本次开票金额 - 发票类型
        if($set['modenum'] == 'piao_kai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $qianpiaotype_name = (new OptService())->get_base_showname('cai_qianpiaotype', $minfo['qianpiaotype']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元'.' — '.$qianpiaotype_name;
        }
        //跨区域涉税事项 - 项目名称 - 本次开具跨区域涉税金额
        if($set['modenum'] == 'engin_kua'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //工程进项票登记	流程名称 - 工程名称 - 本次进项票金额
        if($set['modenum'] == 'piao_shou'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['money'].'元';
        }
        //日常进项票收票登记    流程名称 - 事项名称 - 票款方式 - 本次进项票金额
        if($set['modenum'] == 'piao_day'){
            $kuantype_name = (new OptService())->get_base_showname('cai_kuantype', $minfo['kuantype']);
            $str = $set['name'].' — '.$minfo['title'].' — '.$kuantype_name.' — '.$minfo['money'].'元';
        }
        //项目资料盖章申请	流程名称 - 工程名称 - 资料类型
        if($set['modenum'] == 'engin_stamp'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $zitype = (new OptService())->get_base_showname('scene_zitype', $minfo['zitype']);
            $str = $set['name'].' — '.$engin['name'].' — '.$zitype;
        }
        //工程检查	流程名称 - 工程名称 - 检查类型 - 检查日期
        if($set['modenum'] == 'engin_cha'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $chatype_name = (new OptService())->get_base_showname('engin_chatype', $minfo['chatype']);
            $str = $set['name'].' — '.$engin['name'].' — '.$chatype_name.' — '.$minfo['chadt'];
        }
        //工程档案借阅	流程名称 - 工程名称 - 借阅人 - 用途
        if($set['modenum'] == 'doss_jie'){
            $jieer_name = UserModel::get_realname_by_pk($minfo['jieer']);
            $str = $set['name'].' — '.$jieer_name.' — '.$minfo['yong'];
        }
        //财务档案借阅	流程名称 - 借阅人 - 用途
        if($set['modenum'] == 'coss_jie'){
            $jieer_name = UserModel::get_realname_by_pk($minfo['jieer']);
            $str = $set['name'].' — '.$jieer_name.' — '.$minfo['yong'];
        }
        //行政档案借阅	流程名称 - 借阅人 - 用途
        if($set['modenum'] == 'xoss_jie'){
            $jieer_name = UserModel::get_realname_by_pk($minfo['jieer']);
            $str = $set['name'].' — '.$jieer_name.' — '.$minfo['yong'];
        }

        // [申请人]的[流程名称] - [资料文件名称]等
        if($set['modenum'] == 'seal_gai'){
            $oneName = SealGaiListModel::where(['gaiid'=>$minfo['id']])->value('name');
            $str = $minfo['created_user_name'].'的'.$set['name'].' — '.$oneName.'等';
        }
        // 流程名称 - 申请人 - 事项
        if($set['modenum'] == 'seal_wai'){
            $str = $set['name'].' — '.$minfo['flowuser'].' — '.$minfo['shi'];
        }
        // 流程名称 - 工程名称 - 印章类型
        if($set['modenum'] == 'seal_zhi'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' — '.$engin['name'].' — '.$minfo['yinlei'];
        }


        // 流程名称 - 车牌号 - 经手人
        if($set['modenum'] == 'car_jia'){
            $carinfo = CarModel::get_info_by_pk($minfo['carid']);
            $str = $set['name'].' — '.$carinfo['paihao'].' — '.$minfo['baner'];
        }
        // 流程名称 - 类型 - 内容 - 经手人
        if($set['modenum'] == 'car_xi'){
            $carinfo = CarModel::get_info_by_pk($minfo['carid']);
            $str = $set['name'].' — '.$minfo['type'].' — '.$minfo['baner'];
        }
        // 流程名称 - 车牌号 - 经手人
        if($set['modenum'] == 'car_bao'){
            $carinfo = CarModel::get_info_by_pk($minfo['carid']);
            $str = $set['name'].' — '.$carinfo['paihao'].' — '.$minfo['baner'];
        }


        // 流程名称 - 持证人 - 聘用资格 - 聘费
        if($set['modenum'] == 'cert_employ'){
            $personid_name = CertPersonModel::where(['id'=>$minfo['personid']])->value('name');
            $qualification = array_filter(explode(',',$minfo['qualification']));
            $qualification_name = '';
            foreach ($qualification as $v){
                $qualification_name .= empty($qualification_name) ? (new OptService())->get_base_showname('cert_employ_qualification', $v)
                    : '，'.(new OptService())->get_base_showname('cert_employ_qualification', $v);
            }
            $str = $set['name'].' — '.$personid_name.' — '.$qualification_name.' — '.$minfo['money'].'元';
        }
        //流程名称 - 合同类型 - 合作方 - 合同金额
        if($set['modenum'] == 'contract_xing'){
            $caitype_name = (new OptService())->get_base_showname('engin_xing_caitype', $minfo['caitype']);
            $str = $set['name'].' — '.$caitype_name.' — '.$minfo['gonger'].' — '.$minfo['money'].'元';
        }
        return $str;
    }


    public function create_flow_bidid($modenum, $minfo){
        $bidid = 0;
        if($modenum == 'bid'){
            $bidid = $minfo['id'];
        }
        if($modenum == 'bid_book'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'bid_grant_book'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_jiao'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_fan'){
            $shouinfo = EnginBondShouModel::where(['id'=>$minfo['shouid']])->find();
            if($shouinfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_kai'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'payment_qian'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }

        if($modenum == 'qian_shou'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'qian_zhi'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'qian_piao'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'tou_jiao'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'tou_fan'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'tou_han'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        return $bidid;
    }
    public function create_flow_enginid($modenum, $minfo){
        $enginid = 0;

        if($modenum == 'engin'){
            $enginid = $minfo['id'];
        }
        if($modenum == 'engin_bond_jiao'){
            if($minfo['enginid'] > 0){
                $enginid = $minfo['enginid'];
            }
        }
        if($modenum == 'engin_bond_fan'){
            $shouinfo = EnginBondShouModel::where(['id'=>$minfo['shouid']])->find();
            if($shouinfo['enginid'] > 0){
                $enginid = $shouinfo['enginid'];
            }
        }
        if($modenum == 'engin_bond_kai'){
            if($minfo['enginid'] > 0){
                $enginid = $minfo['enginid'];
            }
        }
        if($modenum == 'payment_bao'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_kai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_kua'){
            $enginid = $minfo['enginid'];
        }

        if($modenum == 'piao_shou'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'hou_gongshui'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'hou_gonghui'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'hou_gongzhi'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_put'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_kou'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_tax'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_neifu'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_yuzhi'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_neikou'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_engin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'income_engin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_junyan'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_junjie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_shi'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'kaoshi'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'kaobao'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_bu'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_cai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_cai_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_shi_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_bian'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_budget'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_budbian'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_cai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_ru'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_shen'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_ling'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_yong'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_jin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_lin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'subbao_shen'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'subbao_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_diary'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_speed'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_chan'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_shui'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_cha'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_stamp'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'visa'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'kuai_ji'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'kuai_shou'){
            $enginid = $minfo['enginid'];
        }
        return $enginid;
    }
    public function create_flow_enginame($modenum, $minfo){
        $id = $this->create_flow_bidid($modenum, $minfo);
        if($id > 0){
            return BidModel::where(['id'=>$id])->value('name');
        }else{
            $id = $this->create_flow_enginid($modenum, $minfo);
            if($id > 0){
                return EnginModel::where(['id'=>$id])->value('name');
            }
        }
        return '';
    }


    public function flow_task_comple_linkage($taskid, $minfo, $bill){
        $task = FlowTaskModel::where(['id'=>$taskid])->find();
        if(!$task){
            return;
        }

        if($task['opt'] == 'add'){//任务类型-加签
            if($task['addcate'] == 1){//被加签人会签
                $qiTask = $this->get_tongjiandson_is_weiwan($task);
                if(!$qiTask){
                    if($task['addtype'] == 1){//同意并加签
                        $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                    }else{//直接加签  =====  上级任务启用
                        $pidTask = FlowTaskModel::where(['id'=>$task['pid']])->find();
                        FlowTaskModel::where(['id'=>$pidTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($pidTask['uid'], 'shen', $bill);
                    }
                }

            }elseif($task['addcate'] == 2){//被加签人或签
                FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid']])->update(['deleted_user'=>1]);
                if($task['addtype'] == 1){//同意并加签
                    $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                }else{//直接加签  =====  上级任务启用
                    $pidTask = FlowTaskModel::where(['id'=>$task['pid']])->find();
                    FlowTaskModel::where(['id'=>$pidTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    (new WorkerService())->add_worker_bill($pidTask['uid'], 'shen', $bill);
                }
            }else{//被加签人依次审批
                $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid']])->where('id','gt',$task['id'])->find();
                if($nextTask){
                    FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);

                }else{
                    $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                }
            }
        }elseif($task['opt'] == 'tui'){
            if($task['signtype'] == 1){//被加签人会签
                if($task['plain'] > 0){
                    $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                        ->where(['pid'=>$task['pid'],'deleted_user'=>0])->where('id','gt',$task['id'])->find();
                    if($nextTask){
                        FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);
                    }else{
                        if($task['tiaotype'] == 1){
                            FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                            $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                            foreach($tingids_uids as $_uid){
                                (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                            }
                        }else{
                            $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                            $dels = implode(',', $delTask);
                            FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                            //跳转下一节点
                            $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                            $this->create_flow_node_task($nextNode, $minfo, $bill);
                        }
                    }
                }else{
                    $qiTask = $this->get_tongjiandson_is_weiwan($task);
                    if(!$qiTask){
                        if($task['tiaotype'] == 1){
                            FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                            $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                            foreach($tingids_uids as $_uid){
                                (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                            }
                        }else{
                            $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                            $dels = implode(',', $delTask);
                            FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                            //跳转下一节点
                            $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                            $this->create_flow_node_task($nextNode, $minfo, $bill);
                        }
                    }
                }
            }
            if($task['signtype'] == 2){//被加签人或签
                $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid'],'deleted_user'=>0])->column('id');
                $dels = implode(',', $delTask);
                FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);


                if($task['tiaotype'] == 1){
                    FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                    foreach($tingids_uids as $_uid){
                        (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                    }
                }else{
                    $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                    $dels = implode(',', $delTask);
                    FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                    //跳转下一节点
                    $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                    $this->create_flow_node_task($nextNode, $minfo, $bill);
                }
            }
        }else{
            if($task['signtype'] == 1){
                if($task['plain'] > 0){
                    $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                        ->where(['pid'=>$task['pid']])->where('id','gt',$task['id'])->find();
                    if($nextTask){
                        FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);
                    }
                }
            }else{
                FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid']])->update(['deleted_user'=>1]);
            }
        }

        return;
    }





    public function get_tongjiandson_is_weiwan($task){
        $exist = false;
        $other = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
            ->where(['pid'=>$task['pid'],'deleted_user'=>0])
            ->where('id','neq', $task['id'])->select()->toArray();
        if(!$task){
            return $exist;
        }

        foreach ($other as $v){
            $exist = $this->check_benson_is_wan($v);
            if($exist){
                break;
            }
        }

        return $exist;
    }

    public function check_benson_is_wan($task){
        if($task['state'] != 2){
            return true;
        }

        $son = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid']])
            ->where(['nodeid'=>$task['nodeid'],'pid'=>$task['id'],'deleted_user'=>0])->select()->toArray();
        foreach ($son as $v){
            return $this->check_benson_is_wan($v);
        }

        return false;
    }


    public function add_flow_task_shen($billid, $nodeid, $taskid, $opt='', $name='', $recall=0, $promer='', $created_at=''){
        if(!empty($promer)){
            FlowPromerModel::insert([
                'comid' => $this->userinfo['comid'],
                'billid' => $billid,
                'uid' => $this->userinfo['id'],
                'domer' => $promer,
                'created_user' => $this->userinfo['id'],
                'created_at' => date("Y-m-d H:i:s"),
            ]);
        }
        return FlowTaskShenModel::insert([
            'comid' => $this->userinfo['comid'],
            'billid' => $billid,
            'nodeid' => $nodeid,
            'taskid' => $taskid,
            'uid' => $this->userinfo['id'],
            'opt' => $opt,
            'name' => $name,
            'recall' => $recall,
            'created_at' => $created_at,
            'updated_at' => date("Y-m-d H:i:s"),
        ]);
    }



    public function get_flow_flowtext($bill, $retype=0){
        $text = '';
        if($bill['isturn'] < 1){
            $text = '<span class="mindianhuang"></span>待申请人提交';
        }else{
            if($bill['status'] == 0){
                $task = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->select()->toArray();
                if(!$task){
                    $text = '<span class="mindianhong"></span>进行中(流程异常)';
                }else{
                    if(count($task) == 1 && $task[0]['nodeid'] == 0){
                        $text = '<span class="mindianhuang"></span>进行中(待申请人重新提交)';
                    }else{
                        $existName = '';
                        foreach ($task as $v){
                            $uname = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$v['uid']])->value('realname');
                            if($uname){
                                if(!$existName){
                                    $existName .= $uname;
                                }else{
                                    $existName .= ','.$uname;
                                }
                            }
                        }
                        if($existName){
                            $text = '<span class="mindianlan"></span>进行中(待 '.$existName.' 审核)';
                        }else{
                            $text = '<span class="mindianhong"></span>进行中(流程异常)';
                        }
                    }
                }
            }

            if($bill['status'] == 1){
                $text = '<span class="mindianlv"></span>审核完成(同意)';
            }

            if($bill['status'] == 2){
                $text = '<span class="mindianhong"></span>审核完成(不同意)';
            }

            if($bill['status'] == 3){
                $text = '<span class="mindianhong"></span>审核完成(超管结束)';
            }

            if($bill['status'] == 5){
                $text = '<span class="mindianhui"></span>审核完成(作废)';
            }
        }

        if($retype > 0){
            $text = strip_tags($text);
        }
        return $text;
    }

    public function get_flow_flowtext_strange($bill){
        $task = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->select()->toArray();
        if(!$task){
            return true;
        }else{
            $existName = '';
            foreach ($task as $v){
                $uname = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$v['uid']])->value('realname');
                if($uname){
                    if(!$existName){
                        $existName .= $uname;
                    }else{
                        $existName .= ','.$uname;
                    }
                }
            }
            if(!$existName){
                return true;
            }
        }
        return false;
    }


    public function unset_flow_node_onshen($nodeid, $uid=0){
        if(!$uid){
            $uid = $this->userinfo['id'];
        }
        $old_onshen = FlowNodeModel::where(['id'=>$nodeid])->value('onshen');
        $old_onshen_arr = explode(',', $old_onshen);
        $uid_key = array_search($uid,$old_onshen_arr);
        if(isset($uid_key)){
            unset($old_onshen_arr[$uid_key]);
        }
        FlowNodeModel::where(['id'=>$nodeid])->update(['onshen'=>implode(',',$old_onshen_arr)]);
        return true;
    }



    public function check_isadvan($modenum){
        $res = false;
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isadvan = isset($step[0]['data']['isadvan']) ? $step[0]['data']['isadvan'] : 0;
        if($isadvan > 0){
            return true;
        }
        return $res;
    }

    public function check_isadvan_edit($modenum, $data){
        $res = false;

        if($data['isturn'] > 0) return $res;

        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isadvan = isset($step[0]['data']['isadvan']) ? $step[0]['data']['isadvan'] : 0;
        if($isadvan > 0){
            return true;
        }
        return $res;
    }


    public function check_isnext($modenum){
        $res = false;
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isnext = isset($step[0]['data']['coursetype']) ? $step[0]['data']['coursetype'] : 0;
        if($isnext > 0){
            return true;
        }
        return $res;
    }


    public function get_flow_show_info($billid=0){
        $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$billid])->find();
        if(!$bill){
            return [];
        }
        $bill['flowtext'] = $this->get_flow_flowtext($bill);

        $bill['but_alone'] = false;
        $bill['alone_page'] = [];

        $bill['but_edit'] = false;
        $bill['but_agree'] = false;
        $bill['but_agreeno'] = false;
        $bill['but_tui'] = false;
        $bill['but_add'] = false;
        $bill['but_zhuan'] = false;
        $bill['but_out'] = false;

        $data['isnext'] = 0;

        if($bill['isturn'] > 0 && $bill['status'] == 0 && $bill['deleted_user'] == 0){
            $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1])
                ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();
            if($myTask){
                $myNode = FlowNodeModel::where(['id'=>$myTask['nodeid']])->find();
                $bill['but_zhuan'] = true;
                if($myTask['opt'] == 'tui' && $myTask['tuiid'] == 0 && $myTask['nodeid'] == 0){
                    $bill['but_edit'] = true;
                    $bill['but_edit_yushen'] = $myTask['tiaotype'];
                }else{
                    if($myNode['type'] == 2){
                        $data['isnext'] = 1;
                    }
                    $bill['but_agree'] = true;
                    $bill['but_agreeno'] = true;
                    $bill['but_tui'] = true;
                    $bill['but_add'] = $myTask['isadd'] > 0 ? true : false;
                    if($myTask['opt'] == 'add'){
                        $bill['but_out'] = true;
                    }
                }

                if($myNode['type'] == 2){
                    $bill['but_add'] = false;
                    $bill['but_zhuan'] = false;
                }

                if($bill['modenum'] == 'hou_gonghui'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    if($minfo['iskou'] == '是'){
                        $bill['but_alone'] = true;
                        $bill['alone_page'] = ['name'=>'编辑费用收取', 'url'=>'../hou_gonghui/kou.php?close=1&id='.$minfo['id']];
                    }
                }

                if($bill['modenum'] == 'hou_dayzhi'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    $bill['but_alone'] = true;
                    $bill['alone_page'] = ['name'=>'编辑款项类型', 'url'=>'../hou_dayzhi/kuan.php?close=1&id='.$minfo['id']];
                }
                if($bill['modenum'] == 'port'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    $bill['but_alone'] = true;
                    $bill['alone_page'] = ['name'=>'编辑', 'url'=>'../port/edit.php?close=1&id='.$minfo['id']];
                }
                if($bill['modenum'] == 'kuai_ji'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    $bill['but_alone'] = true;
                    $bill['alone_page'] = ['name'=>'编辑', 'url'=>'../kuai_ji/edit.php?close=1&id='.$minfo['id']];
                }
                if($bill['modenum'] == 'kuai_shou'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    $bill['but_alone'] = true;
                    $bill['alone_page'] = ['name'=>'编辑', 'url'=>'../kuai_shou/edit.php?close=1&id='.$minfo['id']];
                }
                if($bill['modenum'] == 'peng'){
                    $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();
                    $bill['but_alone'] = true;
                    $bill['alone_page'] = ['name'=>'编辑', 'url'=>'../peng/edit.php?close=1&id='.$minfo['id']];
                }
            }
        }

        $line = [];
        if($bill['isturn'] > 0){
            $line[0] = ['name' => '提交审批','state'=>0,'nodeid'=>0];
            $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
            foreach ($node as $v){
                $line[] = ['name'=>$v['name'], 'state'=>0,'nodeid'=>$v['id']];
            }

            if($bill['status'] == 1){
                foreach ($line as &$item){
                    $item['state'] = 2;
                }
                $line[] = ['name' => '审核完成','state'=>2];
            }else{
                $curTaskNodeid = FlowTaskModel::where(['billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->order('nodeid ASC')->value('nodeid');
                foreach ($line as &$item){
                    if($item['nodeid'] < $curTaskNodeid){
                        $item['state'] = 2;
                    }elseif($item['nodeid'] == $curTaskNodeid){
                        $item['state'] = 1;
                        if(in_array($bill['status'], [2,3,5])){
                            $item['state'] = 2;
                        }
                    }
                }
                $line[] = ['name' => '审核完成','state'=>0];
            }
        }


        $shenlog = FlowShenlogModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        foreach ($shenlog as &$item){
            $user = UserModel::field('realname,avatar,deptid')->where(['id'=>$item['created_user']])->find();
            $item['created_user'] = $user['realname'];
            $item['avatar'] = $user['avatar'];
            $item['created_user_dept'] = DeptModel::where(['id'=>$user['deptid']])->value('name');
            $item['filesarr'] = FileModel::field('name,encpath,ext')->where('id','in',$item['files'])->select()->toArray();
            $item['promer_name'] = UserModel::get_realname_by_ids($item['promer']);
        }

        $talk = FlowTalkModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        foreach ($talk as &$item){
            $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
            $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
            $item['filesarr'] = FileModel::field('name,encpath,ext')->where('id','in',$item['files'])->select()->toArray();
            $item['isdel'] = $item['uid'] == $this->userinfo['id'] ? true : false;
        }

        $data['bill'] = $bill;
        $data['line'] = $line;
        $data['shenlog'] = $shenlog;
        $data['talk'] = $talk;
        $data['isqian'] = $this->cominfo['isqian'];


        $data['flowrelas_info'] = FlowBillModel::field('id,sericnum,modename,modenum,mid')->where('id','in',$bill['flowrelas'])->select()->toArray();
        return $data;
    }



    public function flow_bill_succ($bill){
        $bill = FlowBillModel::where(['id'=>$bill['id']])->find();
        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();


        //处理收付款数据
        if($bill['deleted_user'] == 0 && $bill['isturn'] == 1 && $bill['status'] == 1){
            if($bill['modenum'] == 'qian_shou'){(new KuanService())->qian_shou($bill,$minfo);}
            if($bill['modenum'] == 'hou_gonghui'){(new KuanService())->hou_gonghui($bill,$minfo);}
            if($bill['modenum'] == 'hou_gongyu'){(new KuanService())->hou_gongyu($bill,$minfo);}
            if($bill['modenum'] == 'tou_fan'){(new KuanService())->tou_fan($bill,$minfo);}
            if($bill['modenum'] == 'hou_dayshou'){(new KuanService())->hou_dayshou($bill,$minfo);}
            if($bill['modenum'] == 'account_diao'){
                (new KuanService())->account_shou($bill,$minfo);
                (new KuanService())->account_zhi($bill,$minfo);
            }
            if($bill['modenum'] == 'qian_ban'){(new KuanService())->qian_ban($bill,$minfo);}



            if($bill['modenum'] == 'hou_gongzhi'){(new KuanService())->hou_gongzhi($bill,$minfo);}
            if($bill['modenum'] == 'qian_zhi'){(new KuanService())->qian_zhi($bill,$minfo);}
            if($bill['modenum'] == 'tou_jiao'){(new KuanService())->tou_jiao($bill,$minfo);}
            if($bill['modenum'] == 'hou_bao'){(new KuanService())->hou_bao($bill,$minfo);}
            if($bill['modenum'] == 'hou_gongshui'){(new KuanService())->hou_gongshui($bill,$minfo);}
            if($bill['modenum'] == 'hou_chujin'){(new KuanService())->hou_chujin($bill,$minfo);}
            if($bill['modenum'] == 'hou_chushui'){(new KuanService())->hou_chushui($bill,$minfo);}
            if($bill['modenum'] == 'hou_jie'){(new KuanService())->hou_jie($bill,$minfo);}
            if($bill['modenum'] == 'hou_dayzhi'){(new KuanService())->hou_dayzhi($bill,$minfo);}
        }

        if($bill['deleted_user'] == 0 && $bill['isturn'] == 1 && $bill['status'] == 1){
            if($bill['modenum'] == 'notice'){
                $jieuser = (new XingService())->get_notice_jieer_by_jietype($minfo['jietype'], $minfo['jieer']);
                NoticeModel::where(['id'=>$minfo['id']])->update(['jieuser'=>$jieuser,'fadt'=>date('Y-m-d H:i:s')]);

                $facomName = DeptModel::get_name_by_pk($minfo['facom']);
                $tongtype = (new XingService())->get_notice_tongtype_show($minfo['tongtype']);
                $mess = "[".$facomName."]发布了[".$tongtype."][".$minfo['title']."]，请查看!";
                (new OptService())->add_todo($jieuser, '通知公告',$mess, $minfo['id'], 'notice');
            }


            if($bill['modenum'] == 'qianfa'){
                $facomName = DeptModel::get_name_by_pk($minfo['facom']);
                $mess = "[".$facomName."]发布了[".$minfo['title']."]，请查看！";
                $jieuser = (new XingService())->get_qianfa_jieer_by_jietype($minfo['jietype'], $minfo['jieer']);
                (new OptService())->add_todo($jieuser, '文件签发',$mess, $minfo['id'], 'qianfa');
            }
        }





        if($bill['deleted_user'] == 0 && $bill['isturn'] == 1 && $bill['status'] == 1){
            if($bill['modenum'] == 'engin'){
                (new CertRelevan())->vate_bid_jin_engin_realcert($minfo, $bill);
            }
        }




        if($bill['modenum'] == 'dealcom_geng'){
            (new OtherRelevan())->vate_dealcom($minfo);
        }



        if($bill['modenum'] == 'engin_junyan'){
            (new EnginRelevan())->vate_engin_jundt($minfo);
        }

        if($bill['modenum'] == 'contract_shi'){
            (new EnginRelevan())->vate_contract_shi_bumber($minfo);
        }

        if($bill['modenum'] == 'contract_bu'){
            (new EnginRelevan())->vate_contract_bu_bumber($minfo);
        }

        if($bill['modenum'] == 'contract_cai'){
            (new EnginRelevan())->vate_contract_cai_bumber($minfo);
        }

        if($bill['deleted_user'] == 0 && $bill['isturn'] == 1 && $bill['status'] == 1){
            if($bill['modenum'] == 'hou_jie'){
                (new EnginRelevan())->vate_hou_jie_bumber($minfo);
            }
        }


        if($bill['modenum'] == 'contract_bian'){
            if($minfo['isturn'] == 1 && $minfo['status'] == 1){
                (new EnginRelevan())->vate_contract_cai_list($minfo['id']);
            }
        }

        if($bill['modenum'] == 'engin_budbian'){
            if($minfo['isturn'] == 1 && $minfo['status'] == 1){
                (new EnginRelevan())->vate_engin_budget_list($minfo['id']);
            }
        }


        if($bill['modenum'] == 'cert_perru'){
            (new OtherRelevan())->vate_cert_perru_new($minfo);
        }

        if($bill['modenum'] == 'stuff_ru'){
            (new EnginRelevan())->vate_stuff_ru_create_chu($minfo);
        }

        if($bill['modenum'] == 'cert_use_bid'){
            (new CertRelevan())->set_cert_statueuse_bid_yong($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_use'){
            (new CertRelevan())->set_cert_statueuse_engin_yong($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_employ'){
            (new Cert())->generate_cert_employ_money($minfo['id'],$minfo['pay_type'],$minfo['personid'],$minfo['start_dt'],$minfo['end_dt'],$minfo['money']);
        }

        if($bill['modenum'] == 'cert_borrow'){
            (new CertRelevan())->set_cert_goes_yong($minfo, $bill);
        }

        if($minfo['isturn'] == 1 && $minfo['status'] == 1){
            $step = json_decode($bill['step'], true);
            if(isset($step[0]['data']['tonger']) && !empty($step[0]['data']['tonger'])){
                $mess = "有已通过的审批单待处理：".$bill['summary'];
                (new OptService())->add_todo($step[0]['data']['tonger'], $bill['modename'],$mess, $bill['mid'], $bill['modenum']);

                //同时发起抄送
                if($step[0]['data']['tonger']){
                    FlowPromerModel::insert([
                        'comid' => $this->userinfo['comid'],
                        'billid' => $bill['id'],
                        'uid' => $this->userinfo['id'],
                        'domer' => $step[0]['data']['tonger'],
                        'created_user' => $this->userinfo['id'],
                        'created_at' => date("Y-m-d H:i:s"),
                    ]);
                }
            }
        }

    }
    public function flow_bill_fail($bill){
        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();

        if($bill['modenum'] == 'bid'){
            (new CertRelevan())->set_cert_statueuse_bid($minfo['id']);
        }
        if($bill['modenum'] == 'engin'){
            (new CertRelevan())->set_cert_use_shi_quan($minfo['id']);
            (new CertRelevan())->set_cert_statueuse_engin($minfo['id']);
        }

        if($bill['modenum'] == 'cert_use_bid'){
            (new CertRelevan())->set_cert_statueuse_bid_shi($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_use'){
            (new CertRelevan())->set_cert_statueuse_engin_shi($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_employ'){
            (new Cert())->generate_cert_employ_money($minfo['id'],$minfo['pay_type'],$minfo['personid'],$minfo['start_dt'],$minfo['end_dt'],$minfo['money']);
        }

        if($bill['modenum'] == 'cert_borrow'){
            (new CertRelevan())->set_cert_goes_shi($minfo, $bill);
        }

        if($bill['modenum'] == 'piao_kai'){
            $list = PiaoKaiListModel::where(['kaiid'=>$minfo['id']])->column('id');
            foreach ($list as $listid){
                HouGonghuiListModel::where(['piaoid'=>$listid])->delete();
            }
        }


    }



}
